package com.wz.project.stock.stock.service.impl;

import com.wz.common.utils.text.Convert;
import com.wz.device.entity.BasCargoSpace;
import com.wz.device.service.BasCargoSpaceService;
import com.wz.project.bas.product.mapper.ProductMapper;
import com.wz.project.stock.domain.BasStock;
import com.wz.project.stock.domain.BasStockMaintainRecord;
import com.wz.project.stock.record.mapper.BasStockMaintainRecordMapper;
import com.wz.project.stock.stock.mapper.BasStockMapper;
import com.wz.project.stock.stock.service.IBasStockService;
import jdk.nashorn.internal.parser.TokenStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 库存Service业务层处理
 * 
 * @author wz
 * @date 2021-11-12
 */
@Service
public class BasStockServiceImpl implements IBasStockService 
{
    @Autowired
    private BasStockMapper basStockMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    BasStockMaintainRecordMapper recordMapper;
    @Autowired
    private BasCargoSpaceService cargoSpaceService;
    /**
     * 查询库存
     * 
     * @param stockId 库存ID
     * @return 库存
     */
    @Override
    public BasStock selectBasStockById(String stockId)
    {
        return basStockMapper.selectBasStockById(stockId);
    }

    /**
     * 查询库存列表
     * 
     * @param basStock 库存
     * @return 库存
     */
    @Override
    public List<BasStock> selectBasStockList(BasStock basStock)
    {
        return basStockMapper.selectBasStockList(basStock);
    }

    @Override
    public List<BasStock> selectBasStockList2(BasStock basStock)
    {
        return basStockMapper.selectBasStockList2(basStock);
    }

    /**
     * 新增库存
     * 
     * @param basStock 库存
     * @return 结果
     */
//    @Override
//    public int insertBasStock(BasStock basStock)
//    {
//        return basStockMapper.insertBasStock(basStock);
//    }
    @Override
    public int insertBasStock(BasStock basStock) {
        addStockMaintainRecord("add", basStock);
        return basStockMapper.addStock(basStock);
    }
    /**
     * 修改库存
     * 
     * @param basStock 库存
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBasStock(BasStock basStock)
    {
        addStockMaintainRecord("update", basStock);

        int i = basStockMapper.updateBasStock(basStock);
        //判断货位是否发生变化
        if (i>0 && !basStock.getCargoSpaceIdBef().equals(basStock.getCargoSpaceId())){
            cargoSpaceService.updateBasCargoSpaceStatus(basStock.getCargoSpaceId(),"2");
            //判断原库为是否有库存
            BasStock basStock1 = new BasStock();
            basStock1.setCargoSpaceId(basStock.getCargoSpaceIdBef());
            List<BasStock> basStocks = basStockMapper.selectBasStockList(basStock1);
            if (basStocks.size()<=0){
                cargoSpaceService.updateBasCargoSpaceStatus(basStock.getCargoSpaceIdBef(),"1");
            }
        }


        return i;
    }

    /**
     * 删除库存对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBasStockByIds(String ids)
    {
        String[] stockIds = Convert.toStrArray(ids);
        for (String stockId : stockIds) {
            BasStock basStock = basStockMapper.findStockListOne(stockId);
            addStockMaintainRecord("remove", basStock);
        }
        return basStockMapper.removeStock(stockIds);
    }


    @Override
    public BasStock selectBasStockByTray(String trayCode, String productId) {
        return basStockMapper.selectBasStockByTrayz(trayCode,productId);
    }
    @Override
    public BasStock selectBasStockByTray(String trayCode) {
        return basStockMapper.selectBasStockByTrays(trayCode);
    }
    @Override
    public List<BasCargoSpace> findCargoList(BasCargoSpace basCargoSpace) {
        return basStockMapper.findCargoList(basCargoSpace);
    }
    public void addStockMaintainRecord(String method,BasStock basStock) {
        BasStock basStock1 = basStockMapper.selectBasStockById(basStock.getStockId());
        BasStockMaintainRecord maintainRecord= new BasStockMaintainRecord();
        maintainRecord.setWhGuid(UUID.randomUUID().toString());
        maintainRecord.setStockId(basStock.getStockId());
        maintainRecord.setCargoSpaceId(basStock.getCargoSpaceId());
        maintainRecord.setTrayCode(basStock.getTrayCode());
        maintainRecord.setProductId(basStock.getProductId());
        maintainRecord.setProductName(basStock.getStackingId());
        maintainRecord.setLotNo(basStock.getLotNo());
        if (basStock1 != null){
            maintainRecord.setOldQuantity(String.valueOf(basStock1.getQuantity()));
        }else {
            maintainRecord.setOldQuantity("0");
        }

        maintainRecord.setQuantity(String.valueOf(basStock.getQuantity()));
        maintainRecord.setMethod(method);
        maintainRecord.setWarehouseId(basStock.getWarehouseId());
        maintainRecord.setPrintDate(basStock.getClassNowDateTime());
        maintainRecord.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        recordMapper.insertBasStockMaintainRecord(maintainRecord);
    }
}
